package com.xkstream.client.ui;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.base.WatcherAdapter;
import com.xkstream.client.presenter.ResetPwdContract;
import com.xkstream.client.presenter.ResetPwdPresenter;
import com.xkstream.client.utils.DeviceUtils;
import com.xkstream.client.utils.Utils;
import com.xkstream.client.view.ResizeLayout;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.internal.Util;

import static com.xkstream.client.utils.Utils.checkEmail;

/**
 * Created by xiaoyi on 2017-9-23.
 */

public class ResetPwdActivity extends BaseActivity<ResetPwdContract.Presenter> implements
        ResetPwdContract.View {
    @BindView(R.id.edt_account)
    EditText edtAccount;
    @BindView(R.id.edt_email)
    EditText edtEmail;
    @BindView(R.id.btn_apply)
    TextView btnApply;
    @BindView(R.id.tv_remembered)
    TextView tvRemembered;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    @BindView(R.id.root)
    ResizeLayout root;

    @BindColor(R.color.login_input_selected)
    int colorInputSelected;
    @BindColor(R.color.login_input_unselected)
    int colorInputUnselected;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_resetpwd);
        ButterKnife.bind(this);
        presenter = new ResetPwdPresenter(this);
        initListener();
    }

    private void initListener() {
        edtAccount.setOnFocusChangeListener((v, focus) -> {
            edtAccount.setCompoundDrawablesWithIntrinsicBounds(
                    focus ? R.drawable.nick_selected : R.drawable.nick_unselected,
                    0, 0, 0);
            edtAccount.setBackgroundResource(focus ? R.drawable.login_input_selected
                    : R.drawable.login_input_unselected);
            edtAccount.setTextColor(focus ? colorInputSelected : colorInputUnselected);
            edtAccount.setHintTextColor(focus ? colorInputSelected : colorInputUnselected);
        });

        edtEmail.setOnFocusChangeListener((v, focus) -> {
            edtEmail.setCompoundDrawablesWithIntrinsicBounds(
                    focus ? R.drawable.email_selected : R.drawable.email_unselected,
                    0, 0, 0);
            edtEmail.setBackgroundResource(focus ? R.drawable.login_input_selected
                    : R.drawable.login_input_unselected);
            edtEmail.setTextColor(focus ? colorInputSelected : colorInputUnselected);
            edtEmail.setHintTextColor(focus ? colorInputSelected : colorInputUnselected);
        });

        root.setOnKeyboardShowListener(new ResizeLayout.OnkeyboardShowListener() {
            @Override
            public void onKeyboardShow(int height) {
                int[] location = new int[2];
                tvLogin.getLocationOnScreen(location);
                int position = DeviceUtils.deviceHeight(ResetPwdActivity.this) - location[1] - tvLogin.getHeight();
                root.setPadding(0, -(height - position), 0, 0);

            }

            @Override
            public void onKeyboardHide() {
                root.setPadding(0, 0, 0, 0);
            }
        });

        edtAccount.addTextChangedListener(new WatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setBtnState();
            }
        });

        edtEmail.addTextChangedListener(new WatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setBtnState();
            }
        });
    }

    private void setBtnState() {
        if (edtAccount.getText().length() >= 4 && checkEmail(edtEmail.getText().toString())) {
            btnApply.setBackgroundResource(R.drawable.btn_clickable);
        } else {
            btnApply.setBackgroundResource(R.drawable.btn_unclickable);
        }
    }

    @OnClick({R.id.btn_apply, R.id.tv_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_apply:
                resetPassword();
                break;
            case R.id.tv_login:
                jumpToActivity(LoginActivity_V2.class);
                finish();
                break;
        }
    }

    private void resetPassword() {
        if (checkInput()) {
            Map<String, Object> params = new HashMap<>();
            params.put("user_name", edtAccount.getText().toString());
            params.put("email", edtEmail.getText().toString());
            presenter.resetPwd(params);
        }
    }

    private boolean checkInput() {
        if (edtAccount.getText().toString().length() < 4) {
            showTopTips(R.string.account_length);
            return false;
        }
        if (!Utils.checkEmail(edtEmail.getText().toString())) {
            showTopTips(R.string.wrong_email);
            return false;
        }
        return true;
    }

    @Override
    public void setPresenter(ResetPwdContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showResult(BaseData data) {
        if (data.getCode() == 1) {
            showTopTips(R.string.reset_success);
        } else {
            showTopTips(data.getMsg());
        }
    }

}
